package org.lanqiao.oa.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.lanqiao.oa.dao.RoleDao;
import org.lanqiao.oa.dao.RolePermissionRelationDao;
import org.lanqiao.oa.mapper.Role;
import org.lanqiao.oa.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleDao,Role> implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private RolePermissionRelationDao permissionRelationDao;
    /**
     * 添加角色
     * @param role
     * @return
     */
    @Override
    public boolean addRole(Role role) {
        role.setId(UUID.randomUUID().toString().replace("-","").toUpperCase());
        return roleDao.addRole(role)>0;
    }

    /**
     * 删除角色
     * @param ids
     * @return
     */
    @Override
    public Boolean deleteRole(String[] ids) {
        Integer integer = roleDao.deleteRole(ids);
        return integer>0;
    }

    /**
     * 给角色分配权限
     * @param permissionIds
     * @param roleId
     * @return
     */
    @Override
    public boolean assignPermission(String[] permissionIds, String roleId) {
        //删除原有权限
        Integer integer = permissionRelationDao.deletePermissionByRoleId(roleId);
        Integer integer1 = permissionRelationDao.assignPermission(permissionIds, roleId);
        System.out.println(integer>0&&integer1>0);
        return integer>0&&integer1>0;
    }
}
